//
//  MaiVC.m
//  K-Music
//
//  Created by CHUN on 15/12/27.
//  Copyright © 2015年 CHUN. All rights reserved.
//

#import "MaiVC.h"
#import "AFNetworking.h"
#import "YYplayerVC.h"

#import "KMusicViewController.h"
#import "KMusicTableViewCell.h"
#import "UpInMusicViewController.h"
#import "LocalRecordViewCtr.h"
#import "SingWithoutMusicViewCtr.h"
#import "MusicAllAndAloneViewController.h"
#import "KSingingRecordViewCtr.h"
#import "MainAdvertView.h"

#define mainscreenwidht [UIScreen mainScreen].bounds.size.width
#define mainscreenheight [UIScreen mainScreen].bounds.size.height

@interface MaiVC ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
    UITableView * musicTableView;
}
@property ( strong , nonatomic )NSMutableArray * messageArray;

@end

@implementation MaiVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"麦霸";
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"play" highIcon:@"play" target:self action:@selector(play_play)];
    [self initView];
    
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}

- (void) initView
{
    
    NSMutableArray * imageArray = [NSMutableArray arrayWithObjects:@"组-3@2x.png",@"组-2@2x_8.png",@"组-1@3x_54.png",@"组-1@2x.png", nil];
    MainAdvertView * mainAdvert = [[MainAdvertView alloc]initWithFrame:CGRectMake(0, 0, mainScreenWidth, 120) advertsArray:imageArray];
    [self.view addSubview:mainAdvert];
    
    UIView * middleView = [[UIView alloc]initWithFrame:CGRectMake(0, mainAdvert.bounds.size.height, mainscreenwidht, 100)];
    middleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:middleView];
    
    NSArray *imageArr = @[@"diangetai@2x.png",@"组-2@2x.png",@"hand-holding-business-card-copy@2x.png",@"清唱@2x.png"];
    NSArray *titleArr = @[@"点歌台",@"本地录音",@"合唱",@"清唱"];
    for (int i = 0 ; i < imageArr.count ; i++)
    {
        //   NSArray *keys = [middleImageDict allKeys];
        UIControl * middleCtr = [[UIControl alloc]initWithFrame:CGRectMake(mainscreenwidht*i/4, 0, mainscreenwidht/4, 100)];
        middleCtr.tag = i;
        [middleCtr addTarget:self action:@selector(musicMenuCtr:) forControlEvents:(UIControlEventTouchUpInside)];
        [middleView addSubview:middleCtr];
        
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((middleCtr.frame.size.width-60)*0.5, 10, 60, 60)];
        imageView.image = [UIImage imageNamed:imageArr[i]];
        [middleCtr addSubview:imageView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((middleCtr.frame.size.width-80)*0.5, 80, 70, 10)];
        label.text = titleArr[i];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        [middleCtr addSubview:label];
    }
    UIView * recommendView = [[UIView alloc]initWithFrame:CGRectMake(0, mainAdvert.bounds.size.height+100, mainscreenwidht, 30)];
    recommendView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:recommendView];
    
    UIImageView * musicImageView =[[UIImageView alloc]initWithFrame:CGRectMake(15, 9, 12, 12)];
    musicImageView.image = [UIImage imageNamed:@"图层-31@2x.png"];
    [recommendView addSubview:musicImageView];
    
    UILabel * musicLabel = [[UILabel alloc]initWithFrame:CGRectMake(27, 9, 100, 12)];
    musicLabel.text = @"推荐歌曲";
    musicLabel.font = [UIFont systemFontOfSize:14];
    musicLabel.textColor = [UIColor orangeColor];
    [recommendView addSubview:musicLabel];

    musicTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 250, mainscreenwidht, mainscreenheight-363) style:(UITableViewStylePlain)];
    musicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    musicTableView.delegate = self;
    musicTableView.dataSource = self;
    [self.view addSubview:musicTableView];
    
    [self requestData];
}

-(void)requestData{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    NSString*infoStr11=[NSString stringWithFormat:@"/api/song"];
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
      
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSDictionary * bodyDict = [dic objectForKey:@"body"];
        NSArray * contentArray = [bodyDict objectForKey:@"content"];
        _messageArray = [NSMutableArray array];
        for(NSDictionary * contentDict in contentArray){
            HotMusicMsgDataModel *hotMusic = [[HotMusicMsgDataModel alloc]init];
            hotMusic.avatar = [contentDict objectForKey:@"coverImage"];
            hotMusic.name = [contentDict objectForKey:@"name"];
            NSString * str = [NSString stringWithFormat:@"%@",[contentDict objectForKey:@"size"]];
            float s = [str floatValue];
            s = s/1024/1000;
            hotMusic.size = [NSString stringWithFormat:@"%d",(int)s];
            hotMusic.singerName = [contentDict objectForKey:@"singerName"];
            [hotMusic fixedString];
            [_messageArray addObject:hotMusic];
        }
        [musicTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.messageArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KMusicTableViewCell"];
    if ( !cell ) {
        cell = [[KMusicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KMusicTableViewCell"];
    }
    HotMusicMsgDataModel *hotModel = [self.messageArray objectAtIndex:indexPath.row];
    [(KMusicTableViewCell *)cell updataCellViewWithHeadData:hotModel];
    return cell;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 71;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MusicAllAndAloneViewController *allAndAloneViewCtr = [MusicAllAndAloneViewController new];
    allAndAloneViewCtr.titleName = ((HotMusicMsgDataModel*)[_messageArray objectAtIndex:indexPath.row]).singerName;
    [self.navigationController pushViewController:allAndAloneViewCtr animated:YES];
}

- (void) musicMenuCtr:(UIControl *)sender{

    if(sender.tag == 0){
        UpInMusicViewController * upInMusic = [[UpInMusicViewController alloc]init];
        [self.navigationController pushViewController:upInMusic animated:YES];
    }else if(sender.tag == 1){
        [self.navigationController pushViewController:[LocalRecordViewCtr new] animated:YES];
    }else if(sender.tag == 2){
        MusicAllAndAloneViewController *allAndAloneViewCtr = [MusicAllAndAloneViewController new];
        allAndAloneViewCtr.titleName = @"合唱";
        [self.navigationController pushViewController:allAndAloneViewCtr animated:YES];
    }else if(sender.tag == 3){
        [self.navigationController pushViewController:[SingWithoutMusicViewCtr new] animated:YES];
    }
}

- (void)play_play
{
    NSLog(@"play");
//    [MBProgressHUD showErrorWithStatus:@"播放器" toView:self.view];
    YYplayerVC* yy = [[YYplayerVC alloc]init];
    
    yy.hidesBottomBarWhenPushed = YES;
    
    yy.musicData= [User_information conversion].musicData[0];
    
    [yy setTracks:[Track remoteTracks]];
    
    [self.navigationController pushViewController:yy animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
